<template>
    <div class="login-page">
      <div class="header">
        <el-icon><HomeFilled /></el-icon>
        <span class="title">选课管理系统</span>
      </div>
      <div class="login-card">
        <el-card>
          <div class="login-title">
            <el-icon><User /></el-icon>
            <span>登陆</span>
          </div>
          <el-form :model="form" :rules="rules" ref="loginForm" label-width="90px">
            <el-form-item label="用户id" prop="id" required>
              <el-input v-model="form.id" placeholder="请输入用户id" prefix-icon="el-icon-user" />
            </el-form-item>
            <el-form-item label="用户密码" prop="password" required>
              <el-input v-model="form.password" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" />
            </el-form-item>
            <el-form-item label="用户类型" prop="type" required>
              <el-radio-group v-model="form.type">
                <el-radio label="student">学生</el-radio>
                <el-radio label="teacher">老师</el-radio>
                <el-radio label="admin">管理员</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <div style="width: 100%; display: flex;">
                <el-button type="primary" @click="onSubmit">登陆</el-button>
                <el-button @click="onReset" style="margin-left: 100px;">重置</el-button>
              </div>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue'
  import { ElMessage } from 'element-plus'
  import { HomeFilled, User } from '@element-plus/icons-vue'
  import { useRouter } from 'vue-router'
  //new new
  const router = useRouter()
  const loginForm = ref(null)

  const form = ref({
    id: '',
    password: '',
    type: ''  // 'student'、'teacher'、'admin'
  })
  
  const rules = {
    id: [{ required: true, message: '请输入用户id', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    type: [{ required: true, message: '请选择用户类型', trigger: 'change' }]
  }
  
  const handleLogin = () => {
    if (!form.value.type) {
      ElMessage.error('请选择用户类型')
      return
    }
    
    // 保存用户名和用户类型
    localStorage.setItem('username', form.value.id)
    localStorage.setItem('userRole', form.value.type)
    ElMessage.success('登录成功')
    
    // 根据用户类型跳转到不同页面
    switch (form.value.type) {
      case 'student':
        router.push('/student/my-topic')
        break
      case 'teacher':
        router.push('/teacher/add-topic')
        break
      case 'admin':
        router.push('/admin/student-list')
        break
    }
  }

  const onSubmit = () => {
    loginForm.value.validate((valid) => {
      if (valid) {
        handleLogin()
      }
    })
  }
  
  const onReset = () => {
    loginForm.value.resetFields()
  }
  
  const onRegister = () => {
    router.push('/register')
  }
  </script>
  
  <style scoped>
  .login-page {
    min-height: 100vh;
    background-image: url(~@/assets/01.png);
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
  }
  .header {
    width: 100vw;
    height: 56px;
    background: #92e3f7;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    font-weight: bold;
    color: #222;
    letter-spacing: 2px;
  }
  .header .el-icon {
    margin-right: 10px;
    font-size: 28px;
  }
  .login-card {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 80vh;
  }
  .el-card {
    width: 420px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
    border-radius: 8px;
  }
  .login-title {
    text-align: center;
    font-size: 22px;
    margin-bottom: 18px;
    color: #222;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .login-title .el-icon {
    margin-right: 8px;
    font-size: 22px;
  }
  </style>